﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Data;
using System.Configuration;
using System.Xml;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Web;
using System.Collections.Specialized;

namespace Vms.DB
{
    public class Util
    {
        public Util() { }
        DB m_db = new DB();
        public struct TSession
        {
            public string IP;
            public string SessionID;
            public string User_ID;
            public DateTime StartTime;
            public string UserMSISDN;
            public string KeyPress;
            public string ServiceList;
            public int Money;
            public string chargingStatus;
        }
        public struct TCharging
        {
            //MSISDN ,Service_ID ,StartTime,Session_id,ErrorCode,ErrorDescription,Cost,User_ID
            public int Service_ID;
            public string SessionID;
            public DateTime StartTime;
            public string UserMSISDN;
            public int ErrorCode;
            public int Cost;
            public string ErrorDescription;
            public string User_ID;
            public string CDRIndicator;

        }
        public void write_log(string content, int type)
        {
            try
            {
                string pathlog = ConfigurationManager.AppSettings["LogPath"];
                string FILE_NAME = pathlog + DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString() + ".txt";
                StreamWriter ws = File.AppendText(FILE_NAME);
                if (type == 1)
                {
                    ws.WriteLine(DateTime.Now.ToShortTimeString() + ":Warning");
                    ws.WriteLine(DateTime.Now.ToShortTimeString() + ":" + content);

                }
                else if (type == 2)
                {
                    ws.WriteLine(DateTime.Now.ToShortTimeString() + ":Info");
                    ws.WriteLine(DateTime.Now.ToShortTimeString() + ":" + content);
                }
                else
                {
                    ws.WriteLine(DateTime.Now.ToShortTimeString() + ":Error");
                    ws.WriteLine(DateTime.Now.ToShortTimeString() + ":" + content);
                }
                ws.Close();
            }
            catch (Exception ex)
            {
                m_db.InsertErrorLog(ex.ToString(), "Util", "write_log", 2);
            }
        }
        public void WritelogHeader(NameValueCollection coll)
        {
            string returnValue = "";
            int loop1, loop2;

            // Load Header collection into NameValueCollection object.
            // Put the names of all keys into a string array.
            String[] arr1 = coll.AllKeys;
            for (loop1 = 0; loop1 < arr1.Length; loop1++)
            {
                returnValue += "Key: " + arr1[loop1] + "</n>";
                // Get all values under this key.
                String[] arr2 = coll.GetValues(arr1[loop1]);
                for (loop2 = 0; loop2 < arr2.Length; loop2++)
                {
                    returnValue += "Value " + loop2 + ": " + System.Web.HttpContext.Current.Server.HtmlEncode(arr2[loop2]) + "</n>";
                }
            }
            write_log("Header:" + returnValue, 1);

        }
        public string GetHeaderValue(NameValueCollection coll, string Key)
        {
            string returnValue = "";
            int loop1, loop2;
            // Put the names of all keys into a string array.

            String[] arr = coll.AllKeys;
            for (loop1 = 0; loop1 < arr.Length; loop1++)
            {
                // Get all values under this key.
                String[] arr2 = coll.GetValues(arr[loop1]);
                if (arr[loop1].Contains(Key))
                {
                    for (loop2 = 0; loop2 < arr2.Length; loop2++)
                    {
                        returnValue = System.Web.HttpContext.Current.Server.HtmlEncode(arr2[loop2]);
                    }
                }
            }

            return returnValue;
            //string returnValue = "";
            //int loop1, loop2;
            //NameValueCollection coll;

            //// Load Header collection into NameValueCollection object.
            //coll = Request.Headers;

            //// Put the names of all keys into a string array.
            //String[] arr1 = coll.AllKeys;
            //for (loop1 = 0; loop1 < arr1.Length; loop1++)
            //{
            //    returnValue += "Key: " + arr1[loop1] + "<br/>";
            //    // Get all values under this key.
            //    String[] arr2 = coll.GetValues(arr1[loop1]);
            //    for (loop2 = 0; loop2 < arr2.Length; loop2++)
            //    {
            //        returnValue += "Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br/>";
            //    }
            //}

            //m_util.write_log("Header:" + returnValue);

        }
        public string xulyStringXS(string OrgString)
        {
            string result = "";
            OrgString = OrgString.Replace("nghi", "-");
            OrgString = OrgString.Replace("g1", "<br/>1:");
            OrgString = OrgString.Replace("g2", "<br/>2:");
            OrgString = OrgString.Replace("g3", "<br/>3:");
            OrgString = OrgString.Replace("g4", "<br/>4:");
            OrgString = OrgString.Replace("g5", "<br/>5:");
            OrgString = OrgString.Replace("g6", "<br/>6:");
            OrgString = OrgString.Replace("g7", "<br/>7:");
            OrgString = OrgString.Replace("g8", "<br/>8:");
            OrgString = OrgString.Replace("db 6 -", "<br/>BD6:");
            OrgString = OrgString.Replace("db", "<br/>DB:");
            OrgString = OrgString.Replace("ngay ", " ");
            OrgString = OrgString.Replace("thang", "/");
            OrgString = OrgString.Replace("nm", "");
            OrgString = OrgString.Replace(" ", "");
            result = OrgString.Replace("<>", " ");
            return result;
        }
        public string Convert2assi(string source)
        {
            // Create two different encodings.
            Encoding ascii = Encoding.ASCII;
            Encoding unicode = Encoding.Unicode;

            // Convert the string into a byte[].
            byte[] unicodeBytes = unicode.GetBytes(source);

            // Perform the conversion from one encoding to the other.
            byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

            // Convert the new byte[] into a char[] and then into a string.
            // This is a slightly different approach to converting to illustrate
            // the use of GetCharCount/GetChars.
            char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
            ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
            string asciiString = "";//new string(asciiChars);
            asciiString = source;//.ToUpper();
            string convert = "ĂÂÀẰẦÁẮẤẢẲẨÃẴẪẠẶẬỄẼỂẺÉÊÈỀẾẸỆỀÔÒỒƠỜÓỐỚỎỔỞÕỖỠỌỘỢƯÚÙỨỪỦỬŨỮỤỰÌÍỈĨỊ" +
            "ỲÝỶỸỴĐăâàằầáắấảẳẩãẵẫạặậễẽểẻéêèềếẹệôòồơờóốớỏổởõỗỡọộợưúùứừủửũữụựìíỉĩịỳýỷỹỵđ";
            string To = "AAAAAAAAAAAAAAAAAEEEEEEEEEEEEOOOOOOOOOOOOOOOOOUUUUUUUUUUUIIIIIYYYYY" +
            "Daaaaaaaaaaaaaaaaaeeeeeeeeeeeooooooooooooooooouuuuuuuuuuuiiiiiyyyyyd";
            for (int i = 0; i < To.Length; i++)
            {
                asciiString = asciiString.Replace(convert[i], To[i]);
            }
            asciiString = asciiString.Replace("e", "e");
            //// Display the strings created before and after the conversion.
            write_log("Original string: {0}" + source, 1);
            write_log("Ascii converted string:" + asciiString, 1);
            //Encoding ascii = Encoding.ASCII;
            //Encoding unicode = Encoding.Unicode;

            //// Convert the string into a byte[].
            //byte[] unicodeBytes = unicode.GetBytes(source);

            //// Perform the conversion from one encoding to the other.
            //byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
            //char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
            //ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
            //string asciiString = new string(asciiChars);
            return asciiString;
        }
        public bool CheckDateValid(string Input)
        {
            try
            {
                string format = "ddMMyyyy";
                System.Globalization.DateTimeFormatInfo dtfi = new
                System.Globalization.DateTimeFormatInfo();
                dtfi.ShortDatePattern = format;
                DateTime dt = DateTime.ParseExact(Input, "d", dtfi);
                return true;
            }

            catch (Exception e1)
            {
                write_log("CheckDateValid, Date input = " + Input + ", " + e1.Message, 2);
                return false;
            }
        }
    }
}
